package com.example.userserver.controller;


import com.example.server_engineer.controller.AbstractTypedController;
import com.example.server_engineer.controller.utils.R;
import com.example.userserver.entities.CommonResult;
import com.example.userserver.entities.User;
import com.example.userserver.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * @author Administrator
 */
@RestController
@Tag(name = "用户控制器")
@RequestMapping(value = "user")
public class UserController extends AbstractTypedController<User, Long> {
    @Resource
    UserService usrv;

    UserController(UserService userService) {
        this.svcContext = userService;
    }

    @Operation(summary = "登录方法")
    @PostMapping("/login")
    public CommonResult<User> login(String username,String password) {
        User user = usrv.login(username, password);
        if (user != null) {
            return new CommonResult<>(200, "登录成功", user);
        }
        return new CommonResult<>(400, "登录失败", null);
    }

    @Operation(summary = "通过id删除用户")
    @GetMapping("/delete/{id}")
    public void deleteById(@PathVariable Long id) {
        usrv.delete(id);
    }

//    @PostMapping("/setImg")
//    @Operation(summary = "插入图片")
//    public String setImgByUserId(String id, MultipartFile file){
//        return usrv.setImgByUserId(id,file);
//    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public CommonResult<User> register(User u) {
        return usrv.register(u);
//        return user == null;
    }

    @GetMapping("/find/{id}")
    @Operation(summary = "通过id查找用户")
    public R findById(@PathVariable Long id) {
        User user = usrv.getUserById(id);
        if (user != null) {
            return new R(true, user);
        }
        return new R(false, null);

    }

}
